﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Rebar;
using System.Xml;
using static BankSimulation.ATM;

namespace BankSimulation
{
    public partial class Form2 : Form
    {
        private string selectedBank;
        private int accountNumber;
        decimal amount;
        BankData bankData = new BankData();
        Account account = new Account();
        ATM atm = new ATM();

        public Form2(string bank, int accountNumber)
        {
            InitializeComponent();

            selectedBank = bank;
            this.accountNumber = accountNumber;
            account.BigMoneyWithdrawn += HandleBigMoneyWithdrawn;


            DisplayAccountInformation();
        }
        private void HandleBigMoneyWithdrawn(object sender, BigMoneyEventArgs e)
        {
            MessageBox.Show($"警告：从账号 {e.Account.AccountNumber} 取走了大笔金额 {e.Amount:C}");
        }

        // 在构造函数中显示账户信息
        private void DisplayAccountInformation()
        {
            // 根据所选银行和账号查找相应的账户
            if (selectedBank == "Bank A")
            {
                if (bankData.bankA.accounts.TryGetValue(accountNumber, out account) == false)
                {
                    // 如果未找到账户，将 account 设置为 null
                    account = null;
                }
                else
                {
                    account = bankData.bankA.accounts[accountNumber];
                }
            }
            else if (selectedBank == "Bank B")
            {
                if (bankData.bankB.accounts.TryGetValue(accountNumber, out account) == false)
                {
                    // 如果未找到账户，将 account 设置为 null
                    account = null;
                }
                else
                {
                    account = bankData.bankB.accounts[accountNumber];
                }
            }

            // 如果找到账户，显示信息
            if (account != null)
            {
                // 账号、姓名和余额信息显示在对应的文本框中
                textBox1.Text = account.AccountNumber.ToString(); 
                textBox2.Text = account.Name; 
                textBox3.Text = account.Balance.ToString("C");

                // 如果是信用账号，显示信用额度
                if (account is CreditAccount)
                {
                    CreditAccount creditAccount = (CreditAccount)account;
                    textBox5.Text = creditAccount.CreditLimit.ToString("C");
                }
                else
                {
                    textBox5.Text = "该账户不是信用账户";
                }
            }
            else
            {
                // 如果未找到账户，显示错误消息
                MessageBox.Show("找不到账户信息！");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            account.BigMoneyWithdrawn += HandleBigMoneyWithdrawn;
            // 获取取款金额
            if (decimal.TryParse(textBox4.Text, out amount))
            {
                // 执行取款操作
                account.Withdraw(amount);
                MessageBox.Show($"取款成功！银行：{selectedBank}, 账号：{accountNumber}, 金额：{amount:C}");
                textBox3.Text = account.Balance.ToString("C"); 
                if (account is CreditAccount)
                {
                    CreditAccount creditAccount = (CreditAccount)account;
                    textBox5.Text = creditAccount.CreditLimit.ToString("C");
                }
                else
                {
                    textBox5.Text = "该账户不是信用账户";
                }
            }
            else
            {
                MessageBox.Show("请输入有效的取款金额。");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {

            if (decimal.TryParse(textBox4.Text, out amount))
            {
                // 执行取款操作
                account.Depoist(amount);
                MessageBox.Show($"存款成功！银行：{selectedBank}, 账号：{accountNumber}, 金额：{amount:C}");
                textBox3.Text = account.Balance.ToString("C"); 
            }
            else
            {
                MessageBox.Show("请输入有效的存款金额。");
            }
        }
    }
}
